
*** R_kl

**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global INS =  "dm_lag_r dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D"
global spec = "new_kl"
local spec = "$spec"

clear all
use "$working/prowess_wits_estimation_ronly_kl.dta", clear
		
xtset product_id year
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D ///
ln_wIPT_IND_CHN {
	gen lag_`v' = L.`v'
}
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
	drop if lag_`v'==. | `v'==.
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in r lag_r k lag_k l lag_l I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}
			
gmm (dm_r - {rho=0.9}*dm_lag_r - {beta_l=0.7}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) ///
	- {beta_k=0.2}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_r = [beta_k]_cons
gen beta_l_hat_r = [beta_l]_cons
gen beta_lk_hat_r = [beta_lk]_cons
		
gen beta_k_hat_r_se = _se[beta_k:_cons]
gen beta_l_hat_r_se = _se[beta_l:_cons]
gen beta_lk_hat_r_se = _se[beta_lk:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts_r = beta_k_hat_r + beta_l_hat_r
		
lincom [beta_k]_cons+[beta_l]_cons
generate rts_r_se = r(se)
		
gen theta_k_hat_r= beta_k_hat_r + beta_lk_hat_r*(l - k)
gen theta_l_hat_r = beta_l_hat_r + beta_lk_hat_r*(k - l)

gen theta_k_hat_q= theta_k_hat_r/rts_r
gen theta_l_hat_q = theta_l_hat_r/rts_r

generate rts = 1
generate rts_se = 0
		
foreach variable in k_hat_r l_hat_r k_hat_q l_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)

	bysort nic_08_2dig: egen theta_`variable'_ind_mean = mean(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_med = median(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_sd = sd(theta_`variable') 
}

preserve
	contract nic_08_2dig theta_k_hat_q_ind_mean theta_k_hat_q_ind_med theta_l_hat_q_ind_mean theta_l_hat_q_ind_med theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts rts_se
		
	rename _freq Observations
	
	save "$results_fold/by_ind_thetas_R_kl", replace
restore

preserve
	contract theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med  theta_k_hat_q_sd theta_l_hat_q_sd rts rts_se 
		
	rename _freq Observations
	
	save "$results_fold/thetas_R_kl_all", replace
restore


**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag2_l lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag3_l lag3_D_ln_micro_inv lag3_D_micro_inv lag3_I_sq_micro_inv lag3_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag3_D_ln_micro_inv lag3_D_micro_inv lag3_I_sq_micro_inv lag3_I_sq_micro_inv_D"
global INS =  "dm_lag_r dm_lag2_r dm_lag3_r dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D dm_lag2_l dm_lag2_D_ln_micro_inv dm_lag2_D_micro_inv dm_lag2_I_sq_micro_inv dm_lag2_I_sq_micro_inv_D dm_lag3_l dm_lag3_D_ln_micro_inv dm_lag3_D_micro_inv dm_lag3_I_sq_micro_inv dm_lag3_I_sq_micro_inv_D"

clear all
use "$working/prowess_wits_estimation_ronly_kl.dta", clear
		
xtset product_id year
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D ///
ln_wIPT_IND_CHN {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
	gen lag3_`v' = L3.`v'
}
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. | lag3_`v'==. 
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in r lag_r k lag_k l lag_l I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN ///
 lag2_r lag2_k lag2_l lag2_I_sq lag2_ln_shares lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN ///
 lag3_r lag3_k lag3_l lag3_I_sq lag3_ln_shares lag3_ln_IND lag3_ln_IVS lag3_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}


gmm (dm_r - {rho=0.9}*dm_lag_r - {rho2=-0.1}*dm_lag2_r -  {rho3=-0.05}*dm_lag3_r - {beta_l=0.7}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) + {rho2}*{beta_l}*(dm_lag2_l + dm_lag2_ln_shares) + {rho3}*{beta_l}*(dm_lag3_l + dm_lag3_ln_shares)  ///
	- {beta_k=0.2}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) + {rho2}*{beta_k}*(dm_lag2_k+ dm_lag2_ln_shares) + {rho3}*{beta_k}*(dm_lag3_k+ dm_lag3_ln_shares)   ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq + {rho2}*{beta_lk}*dm_lag2_I_sq + {rho3}*{beta_lk}*dm_lag3_I_sq ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_r = [beta_k]_cons
gen beta_l_hat_r = [beta_l]_cons
gen beta_lk_hat_r = [beta_lk]_cons
		
gen beta_k_hat_r_se = _se[beta_k:_cons]
gen beta_l_hat_r_se = _se[beta_l:_cons]
gen beta_lk_hat_r_se = _se[beta_lk:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts_r = beta_k_hat_r + beta_l_hat_r
		
lincom [beta_k]_cons+[beta_l]_cons
generate rts_r_se = r(se)
		
gen theta_k_hat_r= beta_k_hat_r + beta_lk_hat_r*(l - k)
gen theta_l_hat_r = beta_l_hat_r + beta_lk_hat_r*(k - l)

gen theta_k_hat_q= theta_k_hat_r/rts_r
gen theta_l_hat_q = theta_l_hat_r/rts_r

generate rts = 1
generate rts_se = 0
		
foreach variable in k_hat_r l_hat_r k_hat_q l_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)
	
	bysort nic_08_2dig: egen theta_`variable'_ind_mean = mean(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_med = median(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_sd = sd(theta_`variable') 
}

preserve
	contract nic_08_2dig theta_k_hat_q_ind_mean theta_k_hat_q_ind_med theta_l_hat_q_ind_mean theta_l_hat_q_ind_med theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts rts_se
		
	rename _freq Observations
	
	save "$results_fold/by_ind_thetas_R_kl_3lags", replace
restore

preserve
	contract theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med  theta_k_hat_q_sd theta_l_hat_q_sd rts rts_se 
		
	rename _freq Observations
	
	save "$results_fold/thetas_R_kl_3lags_all", replace
restore


global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global INS =  "dm_lag_q_m dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D"

clear all
use "$working/prowess_wits_estimation_qonly_kl.dta", clear
		
xtset product_id year
			
foreach v in q_m k l I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D ///
	ln_wIPT_IND_CHN {
		gen lag_`v' = L.`v'
}
			
foreach v in q_m k l I_sq ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
	drop if lag_`v'==. | `v'==.
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
		local count_year_fe = r(mean)
			
			
foreach v in q_m lag_q_m k lag_k l lag_l I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

			
gmm (dm_q_m - {rho=0.9}*dm_lag_q_m - {beta_l=0.7}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) ///
	- {beta_k=0.2}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN}), ///
instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		
gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_q = [beta_k]_cons
gen beta_l_hat_q = [beta_l]_cons
gen beta_lk_hat_q = [beta_lk]_cons
		
gen beta_k_hat_q_se = _se[beta_k:_cons]
gen beta_l_hat_q_se = _se[beta_l:_cons]
gen beta_lk_hat_q_se = _se[beta_lk:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts = beta_k_hat_q + beta_l_hat_q
		
lincom [beta_k]_cons+[beta_l]_cons
generate rts_se = r(se)
		
gen theta_k_hat_q= beta_k_hat_q + beta_lk_hat_q*(l - k)
gen theta_l_hat_q = beta_l_hat_q + beta_lk_hat_q*(k - l)
		
		
foreach variable in k_hat_q l_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)
	
	bysort nic_08_2dig: egen theta_`variable'_ind_mean = mean(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_med = median(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_sd = sd(theta_`variable') 
}

preserve
	contract nic_08_2dig theta_k_hat_q_ind_mean theta_k_hat_q_ind_med theta_l_hat_q_ind_mean theta_l_hat_q_ind_med theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se
		
	rename _freq Observations
	
	save "$results_fold/by_ind_thetas_Q_kl", replace
restore

preserve
	contract theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med  theta_k_hat_q_sd theta_l_hat_q_sd rts rts_se 
		
	rename _freq Observations
	
	save "$results_fold/thetas_Q_kl_all", replace
restore
*/



use "$clean/prowess_wits_IO.dta", clear
******************************************************************************
********************************** Data Cleaning *****************************
******************************************************************************
rename products_cocode1 co_code1
bysort co_code1 prod_date3: generate nmbr_prods = _N
generate date_string = prod_date3 
generate year2  = substr(date_string,1,4)
generate month2  = substr(date_string,5,2)
generate day2  = substr(date_string,7,2)

keep if ann_rep_months4==12 // Keep firms reporting data for 12 months.
keep if month2=="03" 

destring year, replace

merge m:1 co_code1 year using "$working/perpetual_inventory_capital"
drop if _merge==2
drop _merge

merge m:1 year using "$working/wholesale_priceindex"
drop if _merge==2
drop _merge

*Instead of 100, make base year 1
replace wholesale_priceindex = wholesale_priceindex/100

sum  wholesale_priceindex, detail

generate wL = sa_compensation_to_e_160/wholesale_priceindex
generate pM = sa_rawmat_stores_spa_146/wholesale_priceindex
generate pE = energy_cons_value9/wholesale_priceindex

foreach variable in wL rK {
	drop if `variable'==.
}


keep if nic_08_2dig =="10" | nic_08_2dig =="13" | nic_08_2dig =="17" | nic_08_2dig =="20" | nic_08_2dig =="21"  | nic_08_2dig =="22" | nic_08_2dig =="23" ///
 | nic_08_2dig =="24" | nic_08_2dig =="25" | nic_08_2dig =="26"| nic_08_2dig =="27" | nic_08_2dig =="28" | nic_08_2dig =="29"

generate TC = wL + rK


**Make observation a single firm
bysort co_code1 year: generate count_thing = _n
keep if count_thing==1

foreach variable in wL rK {
	sum `variable', detail
	local p1 = r(p1)
	local p99 = r(p99)
	drop if `variable' < `p1'
	drop if `variable' > `p99'
	
}

preserve
	generate Observations = 1
	collapse (sum) wL rK TC Observations
	generate theta_l_hat_q_mean = wL/TC
	generate theta_k_hat_q_mean = rK/TC
	generate theta_k_hat_q_sd = 0
	generate theta_l_hat_q_sd = 0
		
	generate rts = 1
	generate rts_se = 0

	save "$results_fold/thetas_CS_kl_OPF_all", replace
	
restore


preserve
	generate Observations = 1
	collapse (sum) wL rK TC Observations, by(nic_08_2dig)
	generate theta_l_hat_q_ind_mean = wL/TC
	generate theta_k_hat_q_ind_mean = rK/TC
	generate theta_k_hat_q_ind_sd = 0
	generate theta_l_hat_q_ind_sd = 0
	
	generate rts = 1
	generate rts_se = 0

	save "$results_fold/by_ind_thetas_CS_kl_nic2", replace
	
	foreach variable in theta_l_hat_q theta_k_hat_q {
		sum `variable'_ind_mean
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)
	}
	
	collapse (mean) theta_l_hat_q_mean theta_k_hat_q_mean theta_l_hat_q_sd theta_k_hat_q_sd rts rts_se (sum) Observations 
	
	save "$results_fold/thetas_CS_kl_nic2_all", replace

restore


preserve
	generate Observations = 1
	collapse (sum) wL rK TC Observations, by(nic_08_4dig nic_08_2dig)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q {
		bysort nic_08_2dig: egen `variable'_ind_sd = sd(`variable')
		bysort nic_08_2dig: egen `variable'_ind_mean = mean(`variable')
		summarize `variable'
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)
	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_ind_mean  theta_l_hat_q_ind_mean  theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se ///
	theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd (sum) Observations, by(nic_08_2dig)
	
	**Making sure we don't grab these accidentally
	foreach variable in theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd {
		rename `variable' NO_`variable'
	}
	
	foreach variable in theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd {
		replace `variable' = 0 if `variable'==.
	}
	
	
	save "$results_fold/by_ind_thetas_CS_kl_nic4", replace
	
	foreach variable in theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd {
		rename NO_`variable' `variable'
	}
	
	collapse (mean)  theta_l_hat_q_mean theta_k_hat_q_mean theta_l_hat_q_sd theta_k_hat_q_sd rts rts_se (sum) Observations
	
	save "$results_fold/thetas_CS_kl_nic4_all", replace


restore



preserve
	clear 

	use  "$results_fold/by_ind_thetas_CS_kl_nic2"

	keep nic_08_2 Observations
	generate match = 1

	tempfile t1
	save `t1', replace
restore

preserve

	generate Observations_alt = 1
	collapse (sum) wL rK TC Observations, by(year)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q {
		sum `variable'
		generate `variable'_ind_mean = r(mean)
		generate `variable'_ind_sd = r(sd)

	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_ind_mean  theta_l_hat_q_ind_mean  theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se (sum) Observations_alt
	
	generate match = 1
	
	merge 1:m match using `t1' 
	save "$results_fold/by_ind_thetas_CS_kl_year", replace
restore

preserve

	generate Observations = 1
	collapse (sum) wL rK TC Observations, by(year)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q {
		sum `variable'
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)

	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_mean  theta_l_hat_q_mean  theta_k_hat_q_sd theta_l_hat_q_sd rts rts_se (sum) Observations
	
	save "$results_fold/thetas_CS_kl_year_all", replace
restore
	
	


preserve

	generate Observations = 1
	collapse (sum) wL rK TC Observations, by(year nic_08_2dig)

	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q {
		bysort nic_08_2dig: egen `variable'_ind_sd = sd(`variable')
		bysort nic_08_2dig: egen `variable'_ind_mean = mean(`variable')
		summarize `variable'
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)
	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_ind_mean  theta_l_hat_q_ind_mean  theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se ///
	theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd (sum) Observations, by(nic_08_2dig)
	
	**Making sure we don't grab these accidentally
	foreach variable in theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd {
		rename `variable' NO_`variable'
	}
	
	save "$results_fold/by_ind_thetas_CS_kl_year_nic2", replace
	
	foreach variable in theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd {
		rename NO_`variable' `variable'
	}
	
	collapse (mean)  theta_l_hat_q_mean theta_k_hat_q_mean theta_l_hat_q_sd theta_k_hat_q_sd rts rts_se (sum) Observations
	
	save "$results_fold/thetas_CS_kl_year_nic2_all", replace


restore

*/




**Point Estimates
**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_m lag_e lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_lm lag_I_sq_le lag_I_sq_me lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km lag_I_sq_micro_inv_ke lag_I_sq_micro_inv_D_ke"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km lag_I_sq_micro_inv_ke lag_I_sq_micro_inv_D_ke"
global INS =  "dm_lag_q_m dm_lag_l dm_lag_m dm_lag_e dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D dm_lag_I_sq_lm dm_lag_I_sq_le dm_lag_I_sq_me dm_lag_I_sq_micro_inv_km dm_lag_I_sq_micro_inv_D_km dm_lag_I_sq_micro_inv_ke dm_lag_I_sq_micro_inv_D_ke"

clear all
use "$working/prowess_wits_estimation_qonly_klme.dta", clear
		
xtset product_id year
			
foreach v in q_m k l m e I_sq ln_shares ln_IND ln_IVS ///
	D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base ///
	k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D  ///
	I_sq_km I_sq_lm I_sq_le I_sq_ke I_sq_me ///
	I_sq_micro_inv_km I_sq_micro_inv_D_km I_sq_inv_base_km I_sq_inv_base_D_km ///
	I_sq_micro_inv_ke I_sq_micro_inv_D_ke I_sq_inv_base_ke I_sq_inv_base_D_ke ///
	ln_wIPT_IND_CHN  {
		gen lag_`v' = L.`v'
}
			
foreach v in q_m k l m e I_sq I_sq_km I_sq_lm I_sq_le I_sq_ke I_sq_me ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
		drop if lag_`v'==. | `v'==.
	}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in q_m lag_q_m k lag_k l lag_l m lag_m e lag_e I_sq lag_I_sq ///
	ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ///
	I_sq_km I_sq_lm I_sq_le I_sq_ke I_sq_me lag_I_sq_km lag_I_sq_lm lag_I_sq_le lag_I_sq_ke lag_I_sq_me ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN  {
				egen mean_`v' = mean(`v'), by(up12)
				gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}
			
gmm (dm_q_m - {rho=0.9}*dm_lag_q_m - {beta_l=0.1}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) ///
		- {beta_k=0.1}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) ///
		- {beta_m=0.7}*(dm_m+ dm_ln_shares) + {rho}*{beta_m}*(dm_lag_m+ dm_lag_ln_shares) ///
		- {beta_e=0.1}*(dm_e+ dm_ln_shares) + {rho}*{beta_e}*(dm_lag_e+ dm_lag_ln_shares) ///
		- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq ///
		- {beta_km=0}*dm_I_sq_km + {rho}*{beta_km}*dm_lag_I_sq_km ///
		- {beta_lm=0}*dm_I_sq_lm + {rho}*{beta_lm}*dm_lag_I_sq_lm ///
		- {beta_le=0}*dm_I_sq_le + {rho}*{beta_le}*dm_lag_I_sq_le ///
		- {beta_ke=0}*dm_I_sq_ke + {rho}*{beta_ke}*dm_lag_I_sq_ke ///
		- {beta_me=0}*dm_I_sq_me + {rho}*{beta_me}*dm_lag_I_sq_me ///
		-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN}), ///
		instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		
gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_q = [beta_k]_cons
gen beta_l_hat_q = [beta_l]_cons
gen beta_m_hat_q = [beta_m]_cons
gen beta_e_hat_q = [beta_e]_cons
		
gen beta_lk_hat_q = [beta_lk]_cons
gen beta_km_hat_q = [beta_km]_cons
gen beta_lm_hat_q = [beta_lm]_cons
gen beta_le_hat_q = [beta_le]_cons
gen beta_ke_hat_q = [beta_ke]_cons
gen beta_me_hat_q = [beta_me]_cons
		
gen beta_k_hat_q_se = _se[beta_k:_cons]
gen beta_l_hat_q_se = _se[beta_l:_cons]
gen beta_m_hat_q_se = _se[beta_m:_cons]
gen beta_e_hat_q_se = _se[beta_e:_cons]
		
gen beta_lk_hat_q_se = _se[beta_lk:_cons]
gen beta_km_hat_q_se = _se[beta_km:_cons]
gen beta_lm_hat_q_se = _se[beta_lm:_cons]
gen beta_le_hat_q_se = _se[beta_le:_cons]
gen beta_ke_hat_q_se = _se[beta_ke:_cons]
gen beta_me_hat_q_se = _se[beta_me:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts = beta_k_hat_q + beta_l_hat_q + beta_m_hat_q + beta_e_hat_q
		
lincom [beta_k]_cons+[beta_l]_cons +[beta_m]_cons + [beta_e]_cons
generate rts_se = r(se)
		
gen theta_k_hat_q= beta_k_hat_q + beta_lk_hat_q*(l - k) + beta_km_hat_q*(m - k) + beta_ke_hat_q*(e - k)
gen theta_l_hat_q = beta_l_hat_q + beta_lk_hat_q*(k - l) + beta_lm_hat_q*(m - l) + beta_le_hat_q*(e - l)
gen theta_m_hat_q = beta_m_hat_q + beta_km_hat_q*(k - m) + beta_lm_hat_q*(l - m) + beta_me_hat_q*(e - m)
gen theta_e_hat_q = beta_e_hat_q + beta_le_hat_q*(l - e) + beta_ke_hat_q*(k - e) + beta_me_hat_q*(m - e)
		
foreach variable in k_hat_q l_hat_q m_hat_q e_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)

	bysort nic_08_2dig: egen theta_`variable'_ind_mean = mean(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_med = median(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_sd = sd(theta_`variable') 
}

preserve		
	contract nic_08_2dig theta_k_hat_q_ind_mean theta_k_hat_q_ind_med theta_l_hat_q_ind_mean theta_l_hat_q_ind_med ///
	theta_m_hat_q_ind_mean theta_m_hat_q_ind_med theta_e_hat_q_ind_mean theta_e_hat_q_ind_med  ///
	theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd rts rts_se 

	rename _freq Observations
	
	save "$results_fold/by_ind_thetas_Q_klme", replace
restore

preserve

	contract theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med ///
	theta_m_hat_q_mean theta_m_hat_q_med theta_e_hat_q_mean theta_e_hat_q_med  ///
	theta_k_hat_q_sd theta_l_hat_q_sd theta_m_hat_q_sd theta_e_hat_q_sd rts rts_se 

	rename _freq Observations
	
	save "$results_fold/thetas_Q_klme_all", replace
restore



global Zbase = "lag_l lag_m lag_e lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_lm lag_I_sq_le lag_I_sq_me lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km lag_I_sq_micro_inv_ke lag_I_sq_micro_inv_D_ke  lag2_l lag2_m lag2_e lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag2_I_sq_lm lag2_I_sq_le lag2_I_sq_me lag2_I_sq_micro_inv_km lag2_I_sq_micro_inv_D_km lag2_I_sq_micro_inv_ke lag2_I_sq_micro_inv_D_ke"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km lag_I_sq_micro_inv_ke lag_I_sq_micro_inv_D_ke lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag2_I_sq_micro_inv_km lag2_I_sq_micro_inv_D_km lag2_I_sq_micro_inv_ke lag2_I_sq_micro_inv_D_ke"
global INS =  "dm_lag_q_m dm_lag2_q_m dm_lag_l dm_lag_m dm_lag_e dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D dm_lag_I_sq_lm dm_lag_I_sq_le dm_lag_I_sq_me dm_lag_I_sq_micro_inv_km dm_lag_I_sq_micro_inv_D_km dm_lag_I_sq_micro_inv_ke dm_lag_I_sq_micro_inv_D_ke dm_lag2_l dm_lag2_m dm_lag2_e dm_lag2_D_ln_micro_inv dm_lag2_D_micro_inv dm_lag2_I_sq_micro_inv dm_lag2_I_sq_micro_inv_D dm_lag2_I_sq_lm dm_lag2_I_sq_le dm_lag2_I_sq_me dm_lag2_I_sq_micro_inv_km dm_lag2_I_sq_micro_inv_D_km dm_lag2_I_sq_micro_inv_ke dm_lag2_I_sq_micro_inv_D_ke"
local spec = "new_klme"
	
clear all
use "$working/prowess_wits_estimation_qonly_klme.dta", clear
		
xtset product_id year
			
foreach v in q_m k l m e I_sq ln_shares ln_IND ln_IVS ///
	D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base ///
	k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D  ///
	I_sq_km I_sq_lm I_sq_le I_sq_ke I_sq_me ///
	I_sq_micro_inv_km I_sq_micro_inv_D_km I_sq_inv_base_km I_sq_inv_base_D_km ///
	I_sq_micro_inv_ke I_sq_micro_inv_D_ke I_sq_inv_base_ke I_sq_inv_base_D_ke ///
	ln_wIPT_IND_CHN  {
		gen lag_`v' = L.`v'
		gen lag2_`v' = L2.`v'
}
			
foreach v in q_m k l m e I_sq I_sq_km I_sq_lm I_sq_le I_sq_ke I_sq_me ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
		drop if lag_`v'==. | `v'==. | lag2_`v'==.
	}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in q_m lag_q_m lag2_q_m k lag_k lag2_k l lag_l lag2_l m lag_m lag2_m e lag_e lag2_e I_sq lag_I_sq lag2_I_sq ///
	ln_shares lag_ln_shares lag2_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ///
	I_sq_km I_sq_lm I_sq_le I_sq_ke I_sq_me lag_I_sq_km lag_I_sq_lm lag_I_sq_le lag_I_sq_ke lag_I_sq_me ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN ///
	lag2_I_sq_km lag2_I_sq_lm lag2_I_sq_le lag2_I_sq_ke lag2_I_sq_me lag2_ln_IND lag2_ln_wIPT_IND_CHN {
				egen mean_`v' = mean(`v'), by(up12)
				gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}
			
			

gmm (dm_q_m - {rho=0.9}*dm_lag_q_m - {rho2=0.05}*dm_lag2_q_m - {beta_l=0.1}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) + {rho2}*{beta_l}*(dm_lag2_l + dm_lag2_ln_shares) ///
		- {beta_k=0.1}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) + {rho2}*{beta_k}*(dm_lag2_k+ dm_lag2_ln_shares) ///
		- {beta_m=0.7}*(dm_m+ dm_ln_shares) + {rho}*{beta_m}*(dm_lag_m+ dm_lag_ln_shares) + {rho2}*{beta_m}*(dm_lag2_m+ dm_lag2_ln_shares) ///
		- {beta_e=0.1}*(dm_e+ dm_ln_shares) + {rho}*{beta_e}*(dm_lag_e+ dm_lag_ln_shares)  + {rho2}*{beta_e}*(dm_lag2_e+ dm_lag2_ln_shares) ///
		- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq + {rho2}*{beta_lk}*dm_lag2_I_sq ///
		- {beta_km=0}*dm_I_sq_km + {rho}*{beta_km}*dm_lag_I_sq_km + {rho2}*{beta_km}*dm_lag2_I_sq_km ///
		- {beta_lm=0}*dm_I_sq_lm + {rho}*{beta_lm}*dm_lag_I_sq_lm + {rho2}*{beta_lm}*dm_lag2_I_sq_lm ///
		- {beta_le=0}*dm_I_sq_le + {rho}*{beta_le}*dm_lag_I_sq_le + {rho2}*{beta_le}*dm_lag2_I_sq_le ///
		- {beta_ke=0}*dm_I_sq_ke + {rho}*{beta_ke}*dm_lag_I_sq_ke + {rho2}*{beta_ke}*dm_lag2_I_sq_ke ///
		- {beta_me=0}*dm_I_sq_me + {rho}*{beta_me}*dm_lag_I_sq_me  + {rho2}*{beta_me}*dm_lag2_I_sq_me ///
		-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN}), ///
		instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_q = [beta_k]_cons
gen beta_l_hat_q = [beta_l]_cons
gen beta_m_hat_q = [beta_m]_cons
gen beta_e_hat_q = [beta_e]_cons
		
gen beta_lk_hat_q = [beta_lk]_cons
gen beta_km_hat_q = [beta_km]_cons
gen beta_lm_hat_q = [beta_lm]_cons
gen beta_le_hat_q = [beta_le]_cons
gen beta_ke_hat_q = [beta_ke]_cons
gen beta_me_hat_q = [beta_me]_cons
		
gen beta_k_hat_q_se = _se[beta_k:_cons]
gen beta_l_hat_q_se = _se[beta_l:_cons]
gen beta_m_hat_q_se = _se[beta_m:_cons]
gen beta_e_hat_q_se = _se[beta_e:_cons]
		
gen beta_lk_hat_q_se = _se[beta_lk:_cons]
gen beta_km_hat_q_se = _se[beta_km:_cons]
gen beta_lm_hat_q_se = _se[beta_lm:_cons]
gen beta_le_hat_q_se = _se[beta_le:_cons]
gen beta_ke_hat_q_se = _se[beta_ke:_cons]
gen beta_me_hat_q_se = _se[beta_me:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts = beta_k_hat_q + beta_l_hat_q + beta_m_hat_q + beta_e_hat_q
		
lincom [beta_k]_cons+[beta_l]_cons +[beta_m]_cons + [beta_e]_cons
generate rts_se = r(se)
		
gen theta_k_hat_q= beta_k_hat_q + beta_lk_hat_q*(l - k) + beta_km_hat_q*(m - k) + beta_ke_hat_q*(e - k)
gen theta_l_hat_q = beta_l_hat_q + beta_lk_hat_q*(k - l) + beta_lm_hat_q*(m - l) + beta_le_hat_q*(e - l)
gen theta_m_hat_q = beta_m_hat_q + beta_km_hat_q*(k - m) + beta_lm_hat_q*(l - m) + beta_me_hat_q*(e - m)
gen theta_e_hat_q = beta_e_hat_q + beta_le_hat_q*(l - e) + beta_ke_hat_q*(k - e) + beta_me_hat_q*(m - e)
		
foreach variable in k_hat_q l_hat_q m_hat_q e_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)

	bysort nic_08_2dig: egen theta_`variable'_ind_mean = mean(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_med = median(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_sd = sd(theta_`variable') 
}

preserve		
	contract nic_08_2dig theta_k_hat_q_ind_mean theta_k_hat_q_ind_med theta_l_hat_q_ind_mean theta_l_hat_q_ind_med ///
	theta_m_hat_q_ind_mean theta_m_hat_q_ind_med theta_e_hat_q_ind_mean theta_e_hat_q_ind_med  ///
	theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd rts rts_se 

	rename _freq Observations
	
	save "$results_fold/by_ind_thetas_Q_klme_2lags", replace
restore

preserve

	contract theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med ///
	theta_m_hat_q_mean theta_m_hat_q_med theta_e_hat_q_mean theta_e_hat_q_med  ///
	theta_k_hat_q_sd theta_l_hat_q_sd theta_m_hat_q_sd theta_e_hat_q_sd rts rts_se 

	rename _freq Observations
	
	save "$results_fold/thetas_Q_klme_2lags_all", replace
restore



use "$clean/prowess_wits_IO.dta", clear
******************************************************************************
********************************** Data Cleaning *****************************
******************************************************************************
rename products_cocode1 co_code1
bysort co_code1 prod_date3: generate nmbr_prods = _N
generate date_string = prod_date3 
generate year2  = substr(date_string,1,4)
generate month2  = substr(date_string,5,2)
generate day2  = substr(date_string,7,2)

keep if ann_rep_months4==12 // Keep firms reporting data for 12 months.
keep if month2=="03" 

destring year, replace

merge m:1 co_code1 year using "$working/perpetual_inventory_capital"
drop if _merge==2
drop _merge

merge m:1 year using "$working/wholesale_priceindex"
drop if _merge==2
drop _merge

*Instead of 100, make base year 1
replace wholesale_priceindex = wholesale_priceindex/100

sum  wholesale_priceindex, detail

generate wL = sa_compensation_to_e_160/wholesale_priceindex
generate pM = sa_rawmat_stores_spa_146/wholesale_priceindex
generate pE = energy_cons_value9/wholesale_priceindex

foreach variable in wL rK pM pE {
	drop if `variable'==.
}


keep if nic_08_2dig =="10" | nic_08_2dig =="13" | nic_08_2dig =="17" | nic_08_2dig =="20" | nic_08_2dig =="21"  | nic_08_2dig =="22" | nic_08_2dig =="23" ///
 | nic_08_2dig =="24" | nic_08_2dig =="25" | nic_08_2dig =="26"| nic_08_2dig =="27" | nic_08_2dig =="28" | nic_08_2dig =="29"

generate TC = wL + rK + pM + pE


**Make observation a single firm
bysort co_code1 year: generate count_thing = _n
keep if count_thing==1

foreach variable in wL rK pM pE {
	sum `variable', detail
	local p1 = r(p1)
	local p99 = r(p99)
	drop if `variable' < `p1'
	drop if `variable' > `p99'
	
}

preserve
	generate Observations = 1
	collapse (sum) wL rK pM pE TC  Observations
	generate theta_l_hat_q_mean = wL/TC
	generate theta_k_hat_q_mean = rK/TC
	generate theta_m_hat_q_mean = pM/TC
	generate theta_e_hat_q_mean = pE/TC
	
	generate theta_k_hat_q_sd = 0
	generate theta_l_hat_q_sd = 0
	generate theta_m_hat_q_sd = 0
	generate theta_e_hat_q_sd = 0
		
	generate rts = 1
	generate rts_se = 0

	save "$results_fold/thetas_CS_klme_OPF_all", replace
	
restore


preserve
	generate Observations = 1
	collapse (sum) wL rK pM pE TC Observations, by(nic_08_2dig)
	generate theta_l_hat_q_ind_mean = wL/TC
	generate theta_k_hat_q_ind_mean = rK/TC
	generate theta_m_hat_q_ind_mean = pM/TC
	generate theta_e_hat_q_ind_mean = pE/TC
	
	generate theta_k_hat_q_ind_sd = 0
	generate theta_l_hat_q_ind_sd = 0
	generate theta_m_hat_q_ind_sd = 0
	generate theta_e_hat_q_ind_sd = 0
	
	generate rts = 1
	generate rts_se = 0
	
	save "$results_fold/by_ind_thetas_CS_klme_nic2", replace
	
	foreach variable in theta_l_hat_q theta_k_hat_q theta_m_hat_q theta_e_hat_q  {
		sum `variable'_ind_mean
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)
	}
	
	collapse (mean) theta_l_hat_q_mean theta_k_hat_q_mean theta_l_hat_q_sd theta_k_hat_q_sd rts rts_se ///
	theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd (sum) Observations 
	
	save "$results_fold/thetas_CS_klme_nic2_all", replace

restore

preserve

	generate Observations = 1
	collapse (sum) wL rK pM pE TC Observations, by(nic_08_4dig nic_08_2dig)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	generate theta_m_hat_q = pM/TC
	generate theta_e_hat_q = pE/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q theta_m_hat_q theta_e_hat_q  {
		bysort nic_08_2dig: egen `variable'_ind_sd = sd(`variable')
		bysort nic_08_2dig: egen `variable'_ind_mean = mean(`variable')
		summarize `variable'
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)
	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_ind_mean  theta_l_hat_q_ind_mean  theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se ///
	theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd ///
	theta_m_hat_q_ind_mean  theta_e_hat_q_ind_mean  theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd ///
	theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd (sum) Observations, by(nic_08_2dig)
	
	**Making sure we don't grab these accidentally
	foreach variable in theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd ///
	theta_m_hat_q_mean  theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd {
		rename `variable' NO_`variable'
	}
	
	foreach variable in theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd {
		replace `variable' = 0 if `variable'==.
	}
	
	
	save "$results_fold/by_ind_thetas_CS_klme_nic4", replace
	
	foreach variable in theta_k_hat_q_mean theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd ///
	theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd {
		rename NO_`variable' `variable'
	}
	
	collapse (mean)  theta_l_hat_q_mean theta_k_hat_q_mean theta_l_hat_q_sd theta_k_hat_q_sd rts rts_se ///
	theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd (sum) Observations
	
	save "$results_fold/thetas_CS_klme_nic4_all", replace


restore

preserve
	clear 

	use  "$results_fold/by_ind_thetas_CS_klme_nic2"

	keep nic_08_2 Observations
	generate match = 1

	tempfile t1
	save `t1', replace
restore


preserve

	generate Observations_alt = 1
	collapse (sum) wL rK pM pE TC Observations_alt, by(year)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	generate theta_m_hat_q = pM/TC
	generate theta_e_hat_q = pE/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q theta_m_hat_q theta_e_hat_q {
		sum `variable'
		generate `variable'_ind_mean = r(mean)
		generate `variable'_ind_sd = r(sd)

	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_ind_mean  theta_l_hat_q_ind_mean theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se ///
	theta_m_hat_q_ind_mean  theta_e_hat_q_ind_mean theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd  (sum) Observations_alt
	
	generate match = 1
	
	merge 1:m match using `t1' 
	save "$results_fold/by_ind_thetas_CS_klme_year", replace
	

restore

preserve

	generate Observations = 1
	collapse (sum) wL rK pM pE TC Observations, by(year)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	generate theta_m_hat_q = pM/TC
	generate theta_e_hat_q = pE/TC
	
	foreach variable in theta_l_hat_q theta_k_hat_q theta_m_hat_q theta_e_hat_q  {
		sum `variable'
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)

	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_mean  theta_l_hat_q_mean  theta_k_hat_q_sd theta_l_hat_q_sd rts rts_se ///
	 theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd  (sum) Observations
	
	save "$results_fold/thetas_CS_klme_year_all", replace
	

restore


preserve

	generate Observations = 1
	collapse (sum) wL rK pM pE TC Observations, by(year nic_08_2dig)
	generate theta_l_hat_q = wL/TC
	generate theta_k_hat_q = rK/TC
	generate theta_m_hat_q = pM/TC
	generate theta_e_hat_q = pE/TC
	

	foreach variable in theta_l_hat_q theta_k_hat_q theta_m_hat_q theta_e_hat_q  {
		bysort nic_08_2dig: egen `variable'_ind_sd = sd(`variable')
		bysort nic_08_2dig: egen `variable'_ind_mean = mean(`variable')
		summarize `variable'
		generate `variable'_mean = r(mean)
		generate `variable'_sd = r(sd)
	}
	generate rts = 1
	generate rts_se = 0
	
	collapse (mean) theta_k_hat_q_ind_mean  theta_l_hat_q_ind_mean theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd rts rts_se ///
	theta_k_hat_q_mean  theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd ///
	theta_m_hat_q_ind_mean  theta_e_hat_q_ind_mean theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd ///
	theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd (sum) Observations, by(nic_08_2dig)
	

	**Making sure we don't grab these accidentally
	foreach variable in theta_k_hat_q_mean theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd ///
	 theta_m_hat_q_mean  theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd {
		rename `variable' NO_`variable'
	}
	
	save "$results_fold/by_ind_thetas_CS_klme_year_nic2", replace
	
	foreach variable in theta_k_hat_q_mean theta_l_hat_q_mean theta_k_hat_q_sd theta_l_hat_q_sd ///
	 theta_m_hat_q_mean  theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd {
		rename NO_`variable' `variable'
	}
	
	collapse (mean)  theta_l_hat_q_mean theta_k_hat_q_mean theta_l_hat_q_sd theta_k_hat_q_sd rts rts_se ///
	 theta_m_hat_q_mean theta_e_hat_q_mean theta_m_hat_q_sd theta_e_hat_q_sd  (sum) Observations
	
	save "$results_fold/thetas_CS_klme_year_nic2_all", replace
		
restore



global Zbase = "lag_l lag_m lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_lm lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km"
global INS =  "dm_lag_q_m dm_lag_l dm_lag_m dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D  dm_lag_I_sq_lm dm_lag_I_sq_micro_inv_km dm_lag_I_sq_micro_inv_D_km"
local spec = "new_klm"

clear all
use "$working/prowess_wits_estimation_qonly_klm.dta", clear
		
xtset product_id year
			
foreach v in q_m k l m I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D  ///
	I_sq_km I_sq_lm  I_sq_micro_inv_km I_sq_micro_inv_D_km I_sq_inv_base_km I_sq_inv_base_D_km ln_wIPT_IND_CHN {
		gen lag_`v' = L.`v'
}
			
foreach v in q_m k l m I_sq I_sq_km I_sq_lm ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==.
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in q_m lag_q_m k lag_k l lag_l m lag_m I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN ///
 I_sq_km I_sq_lm  lag_I_sq_km lag_I_sq_lm  {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

			
 gmm (dm_q_m - {rho=0.9}*dm_lag_q_m - {beta_l=0.1}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) ///
	- {beta_k=0.1}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) ///
	- {beta_m=0.8}*(dm_m+ dm_ln_shares) + {rho}*{beta_m}*(dm_lag_m+ dm_lag_ln_shares) ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq ///
	- {beta_km=0}*dm_I_sq_km + {rho}*{beta_km}*dm_lag_I_sq_km ///
	- {beta_lm=0}*dm_I_sq_lm + {rho}*{beta_lm}*dm_lag_I_sq_lm ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		
gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_q = [beta_k]_cons
gen beta_l_hat_q = [beta_l]_cons
gen beta_m_hat_q = [beta_m]_cons
		
gen beta_lk_hat_q = [beta_lk]_cons
gen beta_km_hat_q = [beta_km]_cons
gen beta_lm_hat_q = [beta_lm]_cons
		
gen beta_k_hat_q_se = _se[beta_k:_cons]
gen beta_l_hat_q_se = _se[beta_l:_cons]
gen beta_m_hat_q_se = _se[beta_m:_cons]
		
gen beta_lk_hat_q_se = _se[beta_lk:_cons]
gen beta_km_hat_q_se = _se[beta_km:_cons]
gen beta_lm_hat_q_se = _se[beta_lm:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts = beta_k_hat_q + beta_l_hat_q + beta_m_hat_q
		
lincom [beta_k]_cons+[beta_l]_cons +[beta_m]_cons
generate rts_se = r(se)
		
gen theta_k_hat_q= beta_k_hat_q + beta_lk_hat_q*(l - k) + beta_km_hat_q*(m - k)
gen theta_l_hat_q = beta_l_hat_q + beta_lk_hat_q*(k - l) + beta_lm_hat_q*(m - l)
gen theta_m_hat_q = beta_m_hat_q + beta_km_hat_q*(k - m) + beta_lm_hat_q*(l - m)

gen theta_e_hat_q = .
		
foreach variable in k_hat_q l_hat_q m_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)

	bysort nic_08_2dig: egen theta_`variable'_ind_mean = mean(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_med = median(theta_`variable')
	bysort nic_08_2dig: egen theta_`variable'_ind_sd = sd(theta_`variable') 
}

generate theta_e_hat_q_ind_mean =.
generate theta_e_hat_q_ind_med = .
generate theta_e_hat_q_ind_sd = .

generate theta_e_hat_q_mean =.
generate theta_e_hat_q_med = .
generate theta_e_hat_q_sd = .

preserve		
	contract nic_08_2dig theta_k_hat_q_ind_mean theta_k_hat_q_ind_med theta_l_hat_q_ind_mean theta_l_hat_q_ind_med ///
	theta_m_hat_q_ind_mean theta_m_hat_q_ind_med theta_e_hat_q_ind_mean theta_e_hat_q_ind_med  ///
	theta_k_hat_q_ind_sd theta_l_hat_q_ind_sd theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd rts rts_se 

	rename _freq Observations
	
	save "$results_fold/by_ind_thetas_Q_klm", replace
restore

preserve

	contract theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med ///
	theta_m_hat_q_mean theta_m_hat_q_med theta_e_hat_q_mean theta_e_hat_q_med  ///
	theta_k_hat_q_sd theta_l_hat_q_sd theta_m_hat_q_sd theta_e_hat_q_sd rts rts_se 

	rename _freq Observations
	
	save "$results_fold/thetas_Q_klm_all", replace
restore

